<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Rule extends Base
{
	
	public function index()
	{
		if(Request::instance()->isPost())
		{
			$name = Request::instance()->post('name');
			$list = DB::name('rule')->where("model like '%".$name."%'")->order('id desc')->paginate(15);
		}else{
		$list = DB::name('rule')->order('id desc')->paginate(15);
		}
		$page = $list->render();
		$this->assign('page', $page);
		return $this->fetch('index',[
			'list' 		=> $list,
		]);
	}


	public function create()
	{
		if(Request::instance()->isPost()){
			$data = Request::instance()->post();
			$res = DB::name('rule')->insert($data);
			if($res){
				$this->success('权限添加成功',url('index/index/index',array('con'=>'rule','act'=>'index')));
			}else{
				$this->error('权限添加失败');
			}
		}
		$menu = $this->menu();
		return $this->fetch('create',[
			'menu'		=>$menu,
		]);
	}

	public function update($id)
	{
		if(Request::instance()->isPost()){
			$data = Request::instance()->post();
			$res = DB::name('rule')->where('id = '.$id)->update($data);
			if($res){
				$this->success('权限修改成功',url('index/index/index',array('con'=>'rule','act'=>'index')));
			}else{
				$this->error('权限修改失败');
			}
		}
		$menu = $this->menu();
		$data = DB::name('rule')->where('id = '.$id)->find();
		return $this->fetch('create',[
			'menu'		=>	$menu,
			'data'		=>	$data,
		]);
	}


	public function menu()
	{
		$menu = DB::name('sys_menu')->field('menu_name')->where('pid = 0')->order('orderid asc')->select();
		return $menu;
	}
}
?>